﻿Imports TwitterVB2
Imports System.IO.File
Public Class frmFollowing
    'this form is linked with the top menu. DO NOT GET CONFUSED WITH THE OTHER FORM!
    Public tw As New TwitterAPI
    Dim consumer As String = "3rZoMe2v9ZDq5T1JoyVzyw"
    Dim consumersecret As String = "Za0raOo0mDs0xqgmmGxS1YI7hxUdoPmBnGtfFVHBek"
    Public oauthtoken As String
    Public oauthtokensecret As String
    Public intRate As Integer
    Dim lstFollowingCache As New ListBox
    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter

    Private Sub frmFollowing_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    End Sub

    Private Sub frmFollowing_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If e.CloseReason = CloseReason.UserClosing Then
            'The user has requested the form be closed so mimimise to the system tray instead.
            e.Cancel = True
            Me.Visible = False
        End If

    End Sub

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\" & frmLogin.txtUsername.Text & "_OAuthtk.dat")
        'My.Settings.m_strToken = objReader.ReadLine()
        'My.Settings.Save()

        'objReader.Close()

        'Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\" & frmLogin.txtUsername.Text & "_OAuthtkST.dat")
        'My.Settings.m_strTokenSecret = objReader1.ReadLine()
        'My.Settings.Save()

        'objReader1.Close()



        tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        Me.ComboBox3.Items.Clear()
        Me.ComboBox3.Items.Clear()
        Me.ComboBox3.Items.Clear()


        For Each twUser As TwitterUser In tw.Friends

            ComboBox3.Items.Add(twUser.ScreenName & "," & twUser.ID)

        Next

        For Each twList As TwitterList In tw.ListsGet(tw.AccountInformation.ScreenName)

            lstWhitelist.Items.Add(twList.Name)

        Next

        frmLoading.Hide()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '  Form1.TextBox2.Text = Form1.TextBox2.Text & " @" & ComboBox3.Text
        System.Diagnostics.Process.Start("http://twitter.com/" + ComboBox3.Text)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Dispose()
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
        'Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\" & frmLogin.txtUsername.Text & "_OAuthtk.dat")
        'My.Settings.m_strToken = objReader.ReadLine()
        'My.Settings.Save()

        'objReader.Close()

        'Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\" & frmLogin.txtUsername.Text & "_OAuthtkST.dat")
        'My.Settings.m_strTokenSecret = objReader1.ReadLine()
        ' My.Settings.Save()

        ' objReader1.Close()



        tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)



        If ComboBox3.Text = "" Then
            Button2.Enabled = False
            Button3.Enabled = False
        Else
            Button2.Enabled = True
            Button3.Enabled = True
        End If


        'tw.ShowUser(ComboBox1.Items(ComboBox1.Text))
        If ComboBox3.Text = TextBox1.Text Then
            Exit Sub
        Else
            TextBox1.Text = ComboBox3.Text
            Dim twUser As TwitterUser = tw.ShowUser(ComboBox3.Text)
            Me.PictureBox1.ImageLocation = twUser.ProfileImageUrl
        End If


    End Sub
    Private Sub ComboBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox3.TextChanged
        ToolTip1.SetToolTip(PictureBox1, ComboBox3.Text)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\" & frmLogin.txtUsername.Text & "_OAuthtk.dat")
        'My.Settings.m_strToken = objReader.ReadLine()
        ' My.Settings.Save()

        'objReader.Close()

        ' Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\" & frmLogin.txtUsername.Text & "_OAuthtkST.dat")
        'My.Settings.m_strTokenSecret = objReader1.ReadLine()
        'My.Settings.Save()
        '
        'objReader1.Close()



        tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)
        tw.UnFollow(ComboBox3.Text)
        TextBox1.Text = ""
        Me.PictureBox1.ImageLocation = ""
        Button2.Enabled = False
        Button3.Enabled = False

        Me.ComboBox3.Items.Clear()
        Me.ComboBox3.Items.Clear()
        Me.ComboBox3.Items.Clear()


        For Each twUser As TwitterUser In tw.Friends
            ComboBox3.Items.Add(twUser.ScreenName)
        Next


    End Sub

    Private Sub btnCleanFollowing_Click(sender As System.Object, e As System.EventArgs) Handles btnCleanFollowing.Click

        On Error Resume Next

        lstCleanFollowing.Items.Clear()
        btnCleanFollowing.Text = "Plz Wait.."
        btnCleanFollowing.Enabled = False

        tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        Dim z As Integer

        For z = 0 To ComboBox3.Items.Count - 1

            lstFollowingCache.Items.Add(ComboBox3.Items.Item(z))

        Next

        ProgressBar1.Maximum = lstFollowingCache.Items.Count

        tmrCheckFollowing.Enabled = True
        btnCleanFollowing.Text = "Checking.."
        ToolTip1.SetToolTip(btnCleanFollowing, "You have to wait an hour before checking again. This is due to Twitter's Rate Limit.")

        'MsgBox("Found " + lstCleanFollowing.Items.Count + " people that you follow that ARE NOT following you back." + vbNewLine + vbNewLine + vbNewLine + _
        '      " Double click to unfollow a user or check the ones you want to unfollow then click 'Unfollow Checked' when you're done.", MsgBoxStyle.Critical)

    End Sub

    Private Sub lstCleanFollowing_DoubleClick(sender As Object, e As System.EventArgs) Handles lstCleanFollowing.DoubleClick

        If MsgBox("Unfollow this user?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            Dim strData = Split(lstCleanFollowing.Items.Item(lstCleanFollowing.SelectedIndex), ",")

            Dim name, id

            name = strData(0)
            id = strData(1)

            tw.UnFollow(id)

            lstCleanFollowing.Items.RemoveAt(lstCleanFollowing.SelectedIndex)

        End If

        lblCount.Text = lstCleanFollowing.Items.Count

    End Sub

    Private Sub lstCleanFollowing_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstCleanFollowing.SelectedIndexChanged

    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click

        On Error Resume Next

        Dim i As Integer

        Dim unFollowed As Integer = 0

        If MsgBox("Are you sure you want to unfollow " + lstCleanFollowing.CheckedItems.Count + " users?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            For i = 0 To lstCleanFollowing.Items.Count - 1

                If lstCleanFollowing.GetItemChecked(i) = True Then

                    Dim strData = Split(lstCleanFollowing.Items.Item(i), ",")

                    Dim name, id

                    name = strData(0)
                    id = strData(1)

                    tw.UnFollow(id)
                    unFollowed = unFollowed + 1

                    lstCleanFollowing.Items.RemoveAt(i)
                    lblCount.Text = lstCleanFollowing.Items.Count

                End If

            Next

        End If

    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click

        Dim strData = Split(ComboBox3.Items.Item(0), ",")

        Dim name, id

        name = strData(0)
        id = strData(1)

        MsgBox(name + " " + id)
        '543173032
        'lstCleanFollowing.Items.Clear()
        'lstCleanFollowing.Items.Add("tw.Relationship(id, tw.AccountInformation.ID).Source.FollowedBy: " & tw.Relationship(544517731, tw.AccountInformation.ID).Source.FollowedBy)
        'lstCleanFollowing.Items.Add("tw.Relationship(id, tw.AccountInformation.ID).Source.Following: " & tw.Relationship(544517731, tw.AccountInformation.ID).Source.Following)
        'lstCleanFollowing.Items.Add("tw.Relationship(id, tw.AccountInformation.ID).Target.FollowedBy: " & tw.Relationship(544517731, tw.AccountInformation.ID).Target.FollowedBy)
        'lstCleanFollowing.Items.Add("tw.Relationship(id, tw.AccountInformation.ID).Target.Following: " & tw.Relationship(544517731, tw.AccountInformation.ID).Target.Following)


    End Sub

    Private Sub ToolTip1_Popup(sender As System.Object, e As System.Windows.Forms.PopupEventArgs) Handles ToolTip1.Popup

    End Sub

    Private Sub tmrRated_Tick(sender As System.Object, e As System.EventArgs) Handles tmrRated.Tick

        On Error Resume Next

        btnCleanFollowing.Text = "Run F4F Check"
        btnCleanFollowing.Enabled = False
        ToolTip1.RemoveAll()
        tmrRated.Enabled = False

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged

        If CheckBox1.Checked = True Then

            tmrAutoUnfollow.Enabled = True

            If tmrRated.Enabled = False Then

                btnCleanFollowing.PerformClick()

            End If

        Else

            tmrAutoUnfollow.Enabled = False

        End If


    End Sub

    Private Sub tmrCheckFollowing_Tick(sender As System.Object, e As System.EventArgs) Handles tmrCheckFollowing.Tick

        'Dim z As Integer

        Dim check3 ', check4

        If (lstFollowingCache.Items.Count > 0) Then

            Dim strData = Split(lstFollowingCache.Items.Item(0), ",")

            Dim name, id

            name = strData(0)
            id = strData(1)

            'check1 = tw.Relationship(id, tw.AccountInformation.ID).Source.FollowedBy
            'check2 = tw.Relationship(id, tw.AccountInformation.ID).Source.Following
            check3 = tw.Relationship(id, tw.AccountInformation.ID).Target.FollowedBy
            'check4 = tw.Relationship(id, tw.AccountInformation.ID).Target.Following

            'If (check1 = True And check2 = False And check3 = False And check4 = True) Then
            If (check3 = False) Then

                lstCleanFollowing.Items.Add(name & "," & id)

                Dim i As Integer

                For i = 0 To lstCleanFollowing.Items.Count - 1

                    If lstCleanFollowing.Items.Item(i).Equals(name & "," & id) Then

                        lstCleanFollowing.SetItemChecked(i, True)

                    End If

                Next

                lblCount.Text = lstCleanFollowing.Items.Count

            End If

            lstFollowingCache.Items.RemoveAt(0)
            ProgressBar1.Value = ProgressBar1.Value + 1

        Else

            tmrCheckFollowing.Enabled = False
            btnCleanFollowing.Text = "Done!"
            ProgressBar1.Value = ProgressBar1.Maximum

        End If

    End Sub
End Class